package it.controllocredito.backend.persistence.tables;

import java.sql.Date;

import it.controllocredito.backend.persistence.bean.Configuration;

import android.content.ContentValues;
import android.database.Cursor;
import android.util.Log;

public abstract class ConfigurationTable
{
	
	private static final String	log_class_name	= "ConfigurationTable";
	
	private static final String	TABLE_NAME		= "configuration";
	
	public enum Columns
	{
		CONFIGURATION_ID("configurationId", 0), NOME("nome", 1), DATA_CREAZIONE(
				"dataCreazione", 2), AUTORE("autore", 3), VERSIONE("versione",
				4);
		
		String	name;
		int		num;
		
		private Columns(String name, int num)
		{
			this.name = name;
			this.num = num;
		}
	};
	
	public static String getName()
	{
		return TABLE_NAME;
	}
	
	// metodo per ottenere la query di creazione della tablla
	public static String getCreateQuery()
	{
		String createQuery = "CREATE TABLE IF NOT EXISTS "
				+ ConfigurationTable.TABLE_NAME + " ("
				+ Columns.CONFIGURATION_ID.toString()
				+ " integer primary key autoincrement, "
				+ Columns.NOME.toString() + " TEXT not null, "
				+ Columns.DATA_CREAZIONE.toString() + " date not null,"
				+ Columns.AUTORE.toString() + "varchar(20),"
				+ Columns.VERSIONE.toString() + "varchar(10);";
		Log.d(log_class_name, "createQuery:" + createQuery);
		return createQuery;
	}
	
	// crea una nuova riga sul db con tutti i campi
	public static ContentValues createRecord(int conf_id, String nome)
	{
		
		ContentValues cv = new ContentValues();
		cv.put(Columns.CONFIGURATION_ID.toString(), conf_id);
		cv.put(Columns.NOME.toString(), nome);
		cv.put(Columns.DATA_CREAZIONE.toString(),
				new Date(System.currentTimeMillis()).toString());
		return cv;
	}
	
	public static String getKeyColumn()
	{
		return Columns.CONFIGURATION_ID.toString();
	}
	
	public static String[] getColumns()
	{
		String[] arrayTableColumns =
		{ Columns.CONFIGURATION_ID.toString(), Columns.NOME.toString() };
		return arrayTableColumns;
	}
	
	public static Configuration getRecord(Cursor cursor)
	{
		Configuration config = new Configuration();
		config.setConfigurationId(Integer.parseInt(cursor
				.getString(Columns.CONFIGURATION_ID.num)));
		config.setNome(cursor.getString(Columns.NOME.num));
		config.setDataCreazione(Date.valueOf(cursor
				.getString(Columns.DATA_CREAZIONE.num)));
		config.setAutore(cursor.getString(Columns.AUTORE.num));
		config.setVersione(cursor.getString(Columns.VERSIONE.num));
		Log.d(log_class_name, "[" + config.getConfigurationId()
				+ "] configuration: nome:" + config.getNome() + ", data:"
				+ config.getDataCreazione().toString());
		return config;
	}
	
}